# 使用selenium 4.0新版本 语法会更新
import requests
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from chaojiying import Chaojiying_Client
import json


def login():
    # 创建浏览器对象
    # executable_path: 指定浏览器驱动的路径
    service = Service(executable_path='./chromedriver.exe')
    options = webdriver.ChromeOptions()
    web = webdriver.Chrome(service=service, options=options)
    # 打开登录界面
    web.get("https://yoyi.gh.loancloudgh.net/console/#/user/login")
    # 登录界面代码(浏览器渲染)
    html = web.page_source
    # 获取验证码图片元素并截图拍照
    png = web.find_element(By.XPATH,
                           '/html/body/div/div/div[2]/div/div[2]/div[1]/div/form/span/span[2]/img').screenshot_as_png
    # 使用超级鹰识别闪照验证码
    chaojiying = Chaojiying_Client('zhufd0320', 'yangganwei0320\\', '957125')  # 用户中心>>软件ID 生成一个替换 96001
    # chaojiying.PostPic(img, 1902)['pic_str'] 验证码内容
    yzm = chaojiying.PostPic(png, 1902)['pic_str']
    print(yzm)
    # 输入用户名
    web.find_element(By.XPATH,
                     '/html/body/div[1]/div/div[2]/div/div[2]/div[1]/div/form/div[1]/div/div/span/span/input').send_keys(
        "yunying")
    # 输入密码
    web.find_element(By.XPATH,
                     '/html/body/div[1]/div/div[2]/div/div[2]/div[1]/div/form/div[2]/div/div/span/span/input').send_keys(
        "123456")
    # 输入验证码
    web.find_element(By.XPATH, '/html/body/div[1]/div/div[2]/div/div[2]/div[1]/div/form/span/input').send_keys(f"{yzm}")
    # 点击登录
    web.find_element(By.XPATH,
                     '/html/body/div[1]/div/div[2]/div/div[2]/div[1]/div/form/div[4]/div/div/span/button').click()
    # cookies等号后面的值 web.get_cookies()[0]['value']
    value = web.get_cookies()[0]['value']
    return value


# 催收日报页面
def use_token(value, time):
    headers = {
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36 Edg/120.0.0.0",
        "Host": "yoyi.gh.loancloudgh.net",
        "Referer": "https://yoyi.gh.loancloudgh.net/console/",
        "Cookie": f"PHPSESSID={value}",
        "X-Requested-With": "XMLHttpRequest",
        "Accept-Language": "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6"
    }
    params = {
        # 当日时间
        "analysis_date": time,
        "page": 1,
        "pageSize": 40,
        "exeExport": 0,
        # 内部催收
        "urge_company_id": 1
    }
    url = "https://yoyi.gh.loancloudgh.net/v2/analysis/analysisCollectorShowDailyReportV2"
    response = requests.get(url, headers=headers, params=params)
    print(response.status_code)
    print(response.text)
    # 内容 需要循环
    datas = json.loads(response.text)["data"]
    print(
        "阶段" + "\t" + "催收员" + "\t" + "总分配单数" + "\t" + "总分配金额" + "\t" + "全还款催回单数" + "\t" + "全还款催回金额" + "\t" + "部分催回单数" + "\t" + "部分催回金额" + "\t" + "总催回金额" + "\t" + "催回率(金额)")
    # data是每一行数据
    for data in datas:
        # 催收阶段（加纳是催收组）
        csjd = data["urge_stage_name"]
        # 催收员
        csy = data["urge_user_name"]
        # 总分配订单数
        fpdds = data["number_of_collection"]
        # 总分配金额
        fpje = data["amount_of_collection"]
        # 全额催回单数
        qbchds = data["one_collected_count"]

        # 部分催回单数
        bfchds = data["current_part_collect_cases"]
        # 部分催回金额
        bfchje = data["current_part_collect_amount"]
        # 总催回金额
        zchje = data["current_collect_amount"]
        # 全额催回金额
        qbchje = int(float(zchje)) - int(float(bfchje))
        # 总催回率（金额）
        if int(float(zchje)) != 0 and int(float(fpje)) != 0:
            qbchl2 = (int(float(zchje)) / int(float(fpje))) * 100

        else:
            qbchl2 = 0
        print(
            csjd + "\t" + csy + "\t" + f'{fpdds}' + "\t" + f'{fpje}' + "\t" + f'{qbchds}' + "\t" + f'{qbchje}' + "\t" + f'{bfchds}' + "\t" + f'{bfchje}' + "\t" + f'{zchje}' + "\t" + f'{qbchl2:.2f}%')

        # print(data)


value = login()
use_token(value, "2024-02-15")
